
Imports System.Data
Imports System.Data.SqlClient


Namespace Ventas
Public Class Comprobante

public Function Listar(ComprobanteId as integer) as Entity.Ventas.Comprobante

Dim strNameProcedure as string = "up_Ven_Comprobante_obtener"
Dim sqlReader As SqlDataReader = Nothing
Dim sqlCon As SqlConnection = New SqlConnection(Configuracion.Connexion.Cnn)
dim objEnt as   entity.Ventas.Comprobante
Try
            sqlCon.Open()
Dim sqlCmd As SqlCommand = New SqlCommand(strNameProcedure, sqlCon)
With sqlCmd
.CommandType = CommandType.StoredProcedure
                .Parameters.Add(New SqlParameter("@ComprobanteId", SqlDbType.Int)).Value =ComprobanteId
            End With
            sqlReader = sqlCmd.ExecuteReader(CommandBehavior.CloseConnection)
If sqlReader.HasRows Then
While sqlReader.Read
 objEnt= new    entity.Ventas.Comprobante
With objEnt
		.ComprobanteId = sqlReader("ComprobanteId").ToString
		.Codigo = sqlReader("Codigo").ToString
		.Fecha = sqlReader("Fecha").ToString
		.ClienteId = sqlReader("ClienteId").ToString
		.ClienteObs = sqlReader("ClienteObs").ToString
		.TipoId = sqlReader("TipoId").ToString
		.Serie = sqlReader("Serie").ToString
		.Nro = sqlReader("Nro").ToString
		.PagoTipoId = sqlReader("PagoTipoId").ToString
		.SubTotal = sqlReader("SubTotal").ToString
		.TotalDescuento = sqlReader("TotalDescuento").ToString
		.IGv = sqlReader("IGv").ToString
		.Redondeo = sqlReader("Redondeo").ToString
		.Total = sqlReader("Total").ToString
		.Cantidad = sqlReader("Cantidad").ToString
		.Estado = sqlReader("Estado").ToString
End With
  End While
  End IF
 Catch ex As Exception
Throw New Exception(ex.ToString)
Finally
 sqlReader.Dispose()
  sqlReader.Close()
  sqlReader = Nothing
  sqlCon.Dispose()
 sqlCon.Close()
 sqlCon = Nothing
 End Try
return objEnt

End Function


'***************************



public Function Listar(Opcion as string , _
		ComprobanteId as Integer, _
		Codigo as String, _
		Fecha as Long, _
		ClienteId as Integer, _
		ClienteObs as String, _
		TipoId as Integer, _
		Serie as String, _
		Nro as String, _
		PagoTipoId as Integer, _
		SubTotal as Double, _
		TotalDescuento as Double, _
		IGv as Double, _
		Redondeo as Double, _
		Total as Double, _
		Cantidad as Integer, _
		Estado as Integer) _
as  Entity.Ventas.ComprobanteCol

Dim strNameProcedure as string = "up_Ven_Comprobante_listar"
dim objCol as   entity.Ventas.ComprobanteCol
Try
Using conn As New SqlConnection(Configuracion.Connexion.Cnn)
Dim cmd As New SqlCommand(strNameProcedure, conn)
cmd.CommandType = CommandType.StoredProcedure
  cmd.Parameters.Add(New SqlParameter("@opcion", SqlDbType.varchar,50)).Value =Opcion
 cmd.Parameters.Add(New SqlParameter("@ComprobanteId",SqlDbType.INT)).Value =ComprobanteId
 cmd.Parameters.Add(New SqlParameter("@Codigo",SqlDbType.VARCHAR,50)).Value =Codigo
 cmd.Parameters.Add(New SqlParameter("@Fecha",SqlDbType.BIGINT)).Value =Fecha
 cmd.Parameters.Add(New SqlParameter("@ClienteId",SqlDbType.INT)).Value =ClienteId
 cmd.Parameters.Add(New SqlParameter("@ClienteObs",SqlDbType.VARCHAR,100)).Value =ClienteObs
 cmd.Parameters.Add(New SqlParameter("@TipoId",SqlDbType.INT)).Value =TipoId
 cmd.Parameters.Add(New SqlParameter("@Serie",SqlDbType.VARCHAR,50)).Value =Serie
 cmd.Parameters.Add(New SqlParameter("@Nro",SqlDbType.VARCHAR,50)).Value =Nro
 cmd.Parameters.Add(New SqlParameter("@PagoTipoId",SqlDbType.INT)).Value =PagoTipoId
 cmd.Parameters.Add(New SqlParameter("@SubTotal",SqlDbType.DECIMAL,18)).Value =SubTotal
 cmd.Parameters.Add(New SqlParameter("@TotalDescuento",SqlDbType.DECIMAL,18)).Value =TotalDescuento
 cmd.Parameters.Add(New SqlParameter("@IGv",SqlDbType.DECIMAL,18)).Value =IGv
 cmd.Parameters.Add(New SqlParameter("@Redondeo",SqlDbType.DECIMAL,18)).Value =Redondeo
 cmd.Parameters.Add(New SqlParameter("@Total",SqlDbType.DECIMAL,18)).Value =Total
 cmd.Parameters.Add(New SqlParameter("@Cantidad",SqlDbType.SMALLINT)).Value =Cantidad
 cmd.Parameters.Add(New SqlParameter("@Estado",SqlDbType.INT)).Value =Estado
conn.Open()
Using dr As SqlDataReader = cmd.ExecuteReader()
		dim iComprobanteId as integer = dr.GetOrdinal("ComprobanteId")
		dim iCodigo as integer = dr.GetOrdinal("Codigo")
		dim iFecha as integer = dr.GetOrdinal("Fecha")
		dim iClienteId as integer = dr.GetOrdinal("ClienteId")
		dim iClienteObs as integer = dr.GetOrdinal("ClienteObs")
		dim iTipoId as integer = dr.GetOrdinal("TipoId")
		dim iSerie as integer = dr.GetOrdinal("Serie")
		dim iNro as integer = dr.GetOrdinal("Nro")
		dim iPagoTipoId as integer = dr.GetOrdinal("PagoTipoId")
		dim iSubTotal as integer = dr.GetOrdinal("SubTotal")
		dim iTotalDescuento as integer = dr.GetOrdinal("TotalDescuento")
		dim iIGv as integer = dr.GetOrdinal("IGv")
		dim iRedondeo as integer = dr.GetOrdinal("Redondeo")
		dim iTotal as integer = dr.GetOrdinal("Total")
		dim iCantidad as integer = dr.GetOrdinal("Cantidad")
		dim iEstado as integer = dr.GetOrdinal("Estado")
		dim i_usu_cre as integer = dr.GetOrdinal("_usu_cre")
		dim i_fec_cre as integer = dr.GetOrdinal("_fec_cre")
		dim i_usu_mod as integer = dr.GetOrdinal("_usu_mod")
		dim i_fec_mod as integer = dr.GetOrdinal("_fec_mod")
		dim i_flg_eli as integer = dr.GetOrdinal("_flg_eli")

 Dim colCount As Integer = dr.FieldCount
 Dim values As Object() = New Object(colCount - 1) {}

 objCol =  New entity.Ventas.ComprobanteCol
  If dr.HasRows Then
While dr.Read()
dim objEnt =  new entity.Ventas.Comprobante
dr.GetValues(values)

		objEnt.ComprobanteId = values(iComprobanteId)
		objEnt.Codigo = values(iCodigo)
		objEnt.Fecha = values(iFecha)
		objEnt.ClienteId = values(iClienteId)
		objEnt.ClienteObs = values(iClienteObs)
		objEnt.TipoId = values(iTipoId)
		objEnt.Serie = values(iSerie)
		objEnt.Nro = values(iNro)
		objEnt.PagoTipoId = values(iPagoTipoId)
		objEnt.SubTotal = values(iSubTotal)
		objEnt.TotalDescuento = values(iTotalDescuento)
		objEnt.IGv = values(iIGv)
		objEnt.Redondeo = values(iRedondeo)
		objEnt.Total = values(iTotal)
		objEnt.Cantidad = values(iCantidad)
		objEnt.Estado = values(iEstado)
 objCol.Add(objEnt)
 End While
 End if
 End Using
 End Using
 Catch ex As Exception
Throw New Exception(ex.ToString)
Finally
 End Try
return objCol

End Function


'***************************

        Public Function ValidarPostInsert(ByVal ComprobanteId As Integer) As Entity.Ventas.Comprobante


            Dim strNameProcedure As String = "Up_Ven_Comprobante_ValidarPostInsert"
            Dim sqlReader As SqlDataReader = Nothing
            Dim sqlCon As SqlConnection = New SqlConnection(Configuracion.Connexion.Cnn)
            Dim objEnt As Entity.Ventas.Comprobante
            Try
                sqlCon.Open()
                Dim sqlCmd As SqlCommand = New SqlCommand(strNameProcedure, sqlCon)
                With sqlCmd
                    .CommandType = CommandType.StoredProcedure
                    .Parameters.Add(New SqlParameter("@ComprobanteId", SqlDbType.Int)).Value = ComprobanteId
                End With
                sqlReader = sqlCmd.ExecuteReader(CommandBehavior.CloseConnection)
                If sqlReader.HasRows Then
                    While sqlReader.Read
                        objEnt = New Entity.Ventas.Comprobante
                        With objEnt
                            .ComprobanteId = sqlReader("ComprobanteId").ToString
                            .codigo = sqlReader("Codigo").ToString
                            .Total = sqlReader("Total").ToString
                            .Cantidad = sqlReader("Cantidad").ToString
                            .Estado = sqlReader("Estado").ToString
                        End With
                    End While
                End If
            Catch ex As Exception
                Throw New Exception(ex.ToString)
            Finally
                sqlReader.Dispose()
                sqlReader.Close()
                sqlReader = Nothing
                sqlCon.Dispose()
                sqlCon.Close()
                sqlCon = Nothing
            End Try
            Return objEnt

        End Function






        '*************************************



Public  Function Insert(ByVal opcion As String, _
		ComprobanteId as Integer, _
		Codigo as String, _
		Fecha as Long, _
		ClienteId as Integer, _
		ClienteObs as String, _
		TipoId as Integer, _
		Serie as String, _
		Nro as String, _
		PagoTipoId as Integer, _
		SubTotal as Double, _
		TotalDescuento as Double, _
		IGv as Double, _
		Redondeo as Double, _
		Total as Double, _
		Cantidad as Integer, _
		Estado as Integer, _
vUsuario as integer ) As Integer

 dim strNameProcedure as string ="Up_Ven_Comprobante_Insertar"
 Try
Dim intCodigo As Integer = -1
 Using sqlCon As SqlConnection = New SqlConnection(Configuracion.Connexion.Cnn)
Using sqlCmd As SqlCommand = New SqlCommand(strNameProcedure, sqlCon)
With sqlCmd
 .CommandType = CommandType.StoredProcedure
 .Parameters.Add(New SqlParameter("@opcion", SqlDbType.VarChar, 120)).Value = opcion
 .Parameters.Add(New SqlParameter("@ComprobanteId",SqlDbType.INT)).Value =ComprobanteId
 .Parameters.Add(New SqlParameter("@Codigo",SqlDbType.VARCHAR,50)).Value =Codigo
 .Parameters.Add(New SqlParameter("@Fecha",SqlDbType.BIGINT)).Value =Fecha
 .Parameters.Add(New SqlParameter("@ClienteId",SqlDbType.INT)).Value =ClienteId
 .Parameters.Add(New SqlParameter("@ClienteObs",SqlDbType.VARCHAR,100)).Value =ClienteObs
 .Parameters.Add(New SqlParameter("@TipoId",SqlDbType.INT)).Value =TipoId
 .Parameters.Add(New SqlParameter("@Serie",SqlDbType.VARCHAR,50)).Value =Serie
 .Parameters.Add(New SqlParameter("@Nro",SqlDbType.VARCHAR,50)).Value =Nro
 .Parameters.Add(New SqlParameter("@PagoTipoId",SqlDbType.INT)).Value =PagoTipoId
 .Parameters.Add(New SqlParameter("@SubTotal",SqlDbType.DECIMAL,18)).Value =SubTotal
 .Parameters.Add(New SqlParameter("@TotalDescuento",SqlDbType.DECIMAL,18)).Value =TotalDescuento
 .Parameters.Add(New SqlParameter("@IGv",SqlDbType.DECIMAL,18)).Value =IGv
 .Parameters.Add(New SqlParameter("@Redondeo",SqlDbType.DECIMAL,18)).Value =Redondeo
 .Parameters.Add(New SqlParameter("@Total",SqlDbType.DECIMAL,18)).Value =Total
 .Parameters.Add(New SqlParameter("@Cantidad",SqlDbType.SMALLINT)).Value =Cantidad
 .Parameters.Add(New SqlParameter("@Estado",SqlDbType.INT)).Value =Estado
 .Parameters.Add(New SqlParameter("@_usuario", SqlDbType.Int)).Value = vUsuario
 .Parameters.Add(New SqlParameter("@_fecha", SqlDbType.BigInt)).Value = now.tofiletime()
.Parameters.Add(New SqlParameter("@variable_out", SqlDbType.Int))
 .Parameters("@variable_out").Direction = ParameterDirection.Output
 sqlCon.Open()
 .ExecuteNonQuery()
 intCodigo = .Parameters("@variable_out").Value
  Return intCodigo
  End With
 End Using
 End Using
 Catch ex As Exception
  Throw New Exception(ex.Message)
 Return -1
 End Try

End Function


'***************************


Public  Function Update(ByVal opcion As String, _
		ComprobanteId as Integer, _
		Codigo as String, _
		Fecha as Long, _
		ClienteId as Integer, _
		ClienteObs as String, _
		TipoId as Integer, _
		Serie as String, _
		Nro as String, _
		PagoTipoId as Integer, _
		SubTotal as Double, _
		TotalDescuento as Double, _
		IGv as Double, _
		Redondeo as Double, _
		Total as Double, _
		Cantidad as Integer, _
		Estado as Integer, _
vUsuario as integer ) As Integer

 dim strNameProcedure as string ="Up_Ven_Comprobante_Actualizar"
 Try
Dim intCodigo As Integer = -1
 Using sqlCon As SqlConnection = New SqlConnection(Configuracion.Connexion.Cnn)
Using sqlCmd As SqlCommand = New SqlCommand(strNameProcedure, sqlCon)
With sqlCmd
 .CommandType = CommandType.StoredProcedure
 .Parameters.Add(New SqlParameter("@opcion", SqlDbType.VarChar, 120)).Value = opcion
 .Parameters.Add(New SqlParameter("@ComprobanteId",SqlDbType.INT)).Value =ComprobanteId
 .Parameters.Add(New SqlParameter("@Codigo",SqlDbType.VARCHAR,50)).Value =Codigo
 .Parameters.Add(New SqlParameter("@Fecha",SqlDbType.BIGINT)).Value =Fecha
 .Parameters.Add(New SqlParameter("@ClienteId",SqlDbType.INT)).Value =ClienteId
 .Parameters.Add(New SqlParameter("@ClienteObs",SqlDbType.VARCHAR,100)).Value =ClienteObs
 .Parameters.Add(New SqlParameter("@TipoId",SqlDbType.INT)).Value =TipoId
 .Parameters.Add(New SqlParameter("@Serie",SqlDbType.VARCHAR,50)).Value =Serie
 .Parameters.Add(New SqlParameter("@Nro",SqlDbType.VARCHAR,50)).Value =Nro
 .Parameters.Add(New SqlParameter("@PagoTipoId",SqlDbType.INT)).Value =PagoTipoId
 .Parameters.Add(New SqlParameter("@SubTotal",SqlDbType.DECIMAL,18)).Value =SubTotal
 .Parameters.Add(New SqlParameter("@TotalDescuento",SqlDbType.DECIMAL,18)).Value =TotalDescuento
 .Parameters.Add(New SqlParameter("@IGv",SqlDbType.DECIMAL,18)).Value =IGv
 .Parameters.Add(New SqlParameter("@Redondeo",SqlDbType.DECIMAL,18)).Value =Redondeo
 .Parameters.Add(New SqlParameter("@Total",SqlDbType.DECIMAL,18)).Value =Total
 .Parameters.Add(New SqlParameter("@Cantidad",SqlDbType.SMALLINT)).Value =Cantidad
 .Parameters.Add(New SqlParameter("@Estado",SqlDbType.INT)).Value =Estado
 .Parameters.Add(New SqlParameter("@_usuario", SqlDbType.Int)).Value = vUsuario
 .Parameters.Add(New SqlParameter("@_fecha", SqlDbType.BigInt)).Value = now.tofiletime()
.Parameters.Add(New SqlParameter("@variable_out", SqlDbType.Int))
 .Parameters("@variable_out").Direction = ParameterDirection.Output
 sqlCon.Open()
 .ExecuteNonQuery()
 intCodigo = .Parameters("@variable_out").Value
  Return intCodigo
  End With
 End Using
 End Using
 Catch ex As Exception
  Throw New Exception(ex.Message)
 Return -1
 End Try

End Function


'***************************


Public Shared Function Eliminar(ByVal opcion As String, ComprobanteId as integer, vusuario as integer) as integer

 dim strNameProcedure as string ="Up_Ven_Comprobante_Eliminar"
 Try
Dim intCodigo As Integer = -1
 Using sqlCon As SqlConnection = New SqlConnection(Configuracion.Connexion.Cnn)
Using sqlCmd As SqlCommand = New SqlCommand(strNameProcedure, sqlCon)
With sqlCmd
 .CommandType = CommandType.StoredProcedure
 .Parameters.Add(New SqlParameter("@opcion", SqlDbType.VarChar, 120)).Value = opcion
                .Parameters.Add(New SqlParameter("@ComprobanteId", SqlDbType.Int)).Value =ComprobanteId
 .Parameters.Add(New SqlParameter("@_usuario", SqlDbType.Int)).Value = vUsuario
 .Parameters.Add(New SqlParameter("@_fecha", SqlDbType.BigInt)).Value = now.tofiletime()
.Parameters.Add(New SqlParameter("@variable_out", SqlDbType.Int))
 .Parameters("@variable_out").Direction = ParameterDirection.Output
 sqlCon.Open()
 .ExecuteNonQuery()
 intCodigo = .Parameters("@variable_out").Value
  Return intCodigo
  End With
 End Using
 End Using
 Catch ex As Exception
  Throw New Exception(ex.Message)
 Return -1
 End Try

End Function


'***************************



End Class



end namespace

